/**
 * Copyright ©2003~2018年12月11日 szkingdom
 * @author raohj
 * @date 2018年12月11日
 * @file RTestJson.java
 * @comment : please input your comment to this file
 * @version : input version
 * @modify:
 * 		1、 2018年12月11日 由 raohj 新建文件
 */
package com.ccnu.demojava.json;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;

import java.util.ArrayList;
import java.util.List;

/**
 * @author raohj
 * @date 2018年12月11日
 * @version
 * @comment 测试阿里的Json对象转Java对象
 */
public class RTestJson {

	public void testJson() {
		String sJson = "{\"custname\":\"饶海军\",\"cust_Type\":\"个人\",\"idno\":\"证件号码\",\"id_type\":\"证件类型\",\"distributor_code\":\"0034\"}";
		JSONObject jsonO = JSONObject.parseObject(sJson);
		RCustObject cust = JSONObject.toJavaObject(jsonO, RCustObject.class);

		System.out.println(cust.getCustName());
	}

	public static class FundDownsideRiskAns{
		String ReturnMessage;   // 返回值。'OK' - 正常结束，  'xxxxx'-由c函数返回的错误信息。
		Double DownsideRisk;    // 下行风险

		public String getReturnMessage() {
			return ReturnMessage;
		}

		public void setReturnMessage(String returnMessage) {
			ReturnMessage = returnMessage;
		}

		public Double getDownsideRisk() {
			return DownsideRisk;
		}

		public void setDownsideRisk(Double downsideRisk) {
			DownsideRisk = downsideRisk;
		}
	}

	public class KeyRateResult{
		String KeyTerm;
		Double KR01;
		/**
		 * 关键点久期
		 */
		Double KRD;

		public String getKeyTerm() {
			return KeyTerm;
		}

		public void setKeyTerm(String keyTerm) {
			KeyTerm = keyTerm;
		}

		public Double getKR01() {
			return KR01;
		}

		public void setKR01(Double KR01) {
			this.KR01 = KR01;
		}

		public Double getKRD() {
			return KRD;
		}

		public void setKRD(Double KRD) {
			this.KRD = KRD;
		}
	}

	public class CashflowKeyRateDurationAns {
		/**
		 * 债券
		 */
		String BondID;
		@JSONField(name = "KeyRateResults")
		List<KeyRateResult> KeyRateResults = new ArrayList<KeyRateResult>();

		public String getBondID() {
			return BondID;
		}

		public void setBondID(String bondID) {
			BondID = bondID;
		}

		public List<KeyRateResult> getKeyRateResults() {
			return KeyRateResults;
		}

		public void setKeyRateResults(List<KeyRateResult> keyRateResults) {
			KeyRateResults = keyRateResults;
		}

		public void addKeyRateResult(KeyRateResult keyRateResult) {
			KeyRateResults.add(keyRateResult);
		}
	}

	public void testParserArrayEx() {
		String sJson = "{\"code\":0,\"data\":[{\"BondID\":\"180002.IB\",\"KeyRateResults\":[{\"KeyTerm\":\"1m\",\"KR01\":0.000017544480193976142,\"KRD\":0.0016703008362877142},{\"KeyTerm\":\"3m\",\"KR01\":0,\"KRD\":0},{\"KeyTerm\":\"1y\",\"KR01\":0.01012508949346369,\"KRD\":0.963946794743285},{\"KeyTerm\":\"2y\",\"KR01\":0.0005758692287187728,\"KRD\":0.05482492748070043},{\"KeyTerm\":\"3y\",\"KR01\":-0.00002468400531796533,\"KRD\":-0.00235001061699645},{\"KeyTerm\":\"5y\",\"KR01\":0,\"KRD\":0}]}]}";
		JSONObject jsonObject = JSONObject.parseObject(sJson);
		JSONArray jsonArray = jsonObject.getJSONArray("data");
		for (int i=0; i<jsonArray.size(); i++) {
			CashflowKeyRateDurationAns ans = JSONObject.parseObject(jsonArray.get(i).toString(), CashflowKeyRateDurationAns.class);
			System.out.println("DownsideRisk:" + ans.BondID);
		}
	}
	public void testParserArray() {
		String sJson = "{\"code\":0,\"data\":[{\"DownsideRisk\":0.008859862258591642}]}";
		JSONObject jsonObject = JSONObject.parseObject(sJson);
		JSONArray jsonArray = jsonObject.getJSONArray("data");
		for (int i=0; i<jsonArray.size(); i++) {
			FundDownsideRiskAns ans = JSONObject.parseObject(jsonArray.get(i).toString(), FundDownsideRiskAns.class);
			System.out.println("DownsideRisk:" + ans.DownsideRisk);
		}
	}

	public void testHostJson() {
		String jsonString = "{\"head\": {\"msgid\": \"453674068479049728\"},\"body\": {\"SDOROOT\": {\"APP_HEAD\": {\"USER_ID\": \"L9990\",\"BIZ_SEQ_NO\": 99200898},\"SYS_HEAD\": {\"ORG_SYS_ID\": \"020102\",\"RET\": {\"SDO\": {\"RET_CODE\": \"I030006000000\",\"RET_MSG\": \"交易成功完成\"}},\"ESB_SEQ_NO\": \"\",\"SUB_SYSTEM_NO\": \"030006\",\"CONSUMER_SVR_ID\": \"\",\"SERVICE_CODE\": 11003000003,\"CONSUMER_SEQ_NO\": \"0201012019071900000464\",\"TRAN_TIMESTAMP\": \"085007330\",\"CONSUMER_ID\": \"020102\",\"WS_ID\": \"0000000141\",\"SERVICE_SCENE\": \"05\",\"RET_STATUS\": \"S\",\"TRAN_DATE\": 20210917},\"LOCAL_HEAD\": {\"RURAL_BRANCH_ID\": \"0000\",\"BUS_SEQ_NO\": \"\"},\"package_type\": \"xml\",\"BODY\": {\"FINANCE_SRC\": \"\",\"STAT_FLAG\": 1,\"FILE_NAME\": \"\",\"ACCT_BIT\": 1101000000,\"PASSBOOK_NO\": \"\",\"ACCT_ARRAY\": {\"SDO\": {\"SUB_CODE\": 2350101,\"TREM\": 0,\"ACCT_SEQNO\": 10,\"CUR_CODE\": \"CNY\",\"CUR_TYPE\": 0,\"BUS_NAME\": \"1-个人活期存款-个人活期存款\",\"ACCT_NO\": 960001235000152385,\"STAT_FLAG1\": 1,\"FREEZE_AMT\": 0,\"ACCT_BAL\": 569359.05,\"ACCT_BRAN_CODE\": 9600,\"ACCT_BIT1\": 1,\"OPEN_AMT\": 0,\"ADD_INFO\": \"0000001\",\"DRAW_CTRL_BIT\": 0,\"OPEN_BRAN_CODE\": 9600,\"ACCT_ATTR\": 0,\"SAVE_TIMES\": 0,\"FREEZE_FLAG\": 0,\"PROVE_AMT\": 0,\"BAL_FLAG\": 2,\"CANCEL_DATE\": \"\",\"ACCT_ATTR_CODE\": 50,\"STOP_FLAG\": 0,\"LOSS_FLAG\": \"000\",\"ACCT_STATUS\": \"00\",\"CARD_ACCT_SEQNO\": 0,\"OPEN_DATE\": 20181127,\"STOP_PAYMENT_AMT\": 0,\"FUNCTIONARY\": \"\",\"OPEN_BRAN_NAME\": \"广州分行营业部\",\"VAL_ACCT_BAL\": 569359.05}},\"ACCT_CLASS\": 0,\"STD_ACCT_FLAG\": 0,\"CERT_NO\": \"44088219890727017X\",\"OPEN_BRAN_CODE\": 9600,\"ACCT_FLAG\": 0,\"SETTLE_FLAG\": 1,\"RECODE_REC\": 1,\"CERT_TYPE\": 110,\"FREEZE_FLAG\": 0,\"DRAW_MODE\": 1,\"CUST_NO\": \"A0004531708\",\"ACCT_TYPE\": 1,\"RECORDS\": 1,\"CANCEL_DATE\": \"\",\"STOP_FLAG\": 0,\"ACCT_STATUS\": 0,\"LOSS_FLAG\": \"000\",\"OPEN_DATE\": 20171127,\"FUNCTIONARY\": \"\",\"CUST_NAME\": \"赵九\"}}}}";
		JSONObject json = JSONObject.parseObject(jsonString);
		JSONObject resultJson = json.getJSONObject("body").getJSONObject("SDOROOT");

		JSONObject responseHead = resultJson.getJSONObject("SYS_HEAD");
		JSONObject responseBody = resultJson.getJSONObject("BODY");
		String retcode = ((JSONObject) ((JSONObject) responseHead.get("RET")).get("SDO")).getString("RET_CODE");
		String retmsg = ((JSONObject) ((JSONObject) responseHead.get("RET")).get("SDO")).getString("RET_MSG");

		if (responseBody != null) {
			JSONObject responseAcctArray = responseBody.getJSONObject("ACCT_ARRAY");
			if (responseAcctArray.getJSONObject("SDO") instanceof JSONObject) {
				JSONObject responseAcct1 = responseAcctArray.getJSONObject("SDO");
				if (responseAcct1 != null && !responseAcct1.isEmpty()) {
				}
			} else {
				JSONArray responseArr = responseAcctArray.getJSONArray("SDO");
			}
		}
	}
}
